#coding:utf-8
'''
Created on 2017-04-20

@author: sandy
'''

import os
import json
import logging
from pycommon import Utils
from pycommon import RemoteManager

class Config(object):
    
    def __init__(self):
        self.rundir = os.path.dirname(__file__)
        self.game_db_name = u""
        self.game_db_host = u"127.0.0.1"
        self.game_db_port = 3306
        self.game_db_user = u"root"
        self.game_db_passwd = u"1111"
        self.game_db_linkcount = 2

        self.platform_db_name = u""
        self.platform_db_host = u"127.0.0.1"
        self.platform_db_port = 3306
        self.platform_db_user = u"root"
        self.platform_db_passwd = u"1111"
        self.platform_db_linkcount = 2


        self.log_ip = u"127.0.0.1"
        self.log_port = 1240 
        self.server_ip = u"127.0.0.1"
        self.server_port = 0
        self.public_ip = u"127.0.0.1"
        self.public_port = 0
        self.max_client = 0
        self.web_ip = u"127.0.0.1"
        self.web_port = 8023

        self.center_ip = u"127.0.0.1"
        self.center_port = 8300

        self.redisdb_ip =u"127.0.0.1"
        self.redisdb_port=6379
        self.redisdb_db=0
        self.redisdb_passwd=u""

        self.login_manager_ip = u''
        self.login_manager_port = 0

    
    def onRemoteConfig(self, cfgname, jf):
        ctype,cname = cfgname.split(u".")
        if ctype==u'server'+unicode(RemoteManager.instance.sid) and cname==u'serverconfig':
            self.loadFromJson(jf)
     
    def loadFromFile(self):
        cf = json.load(file(os.path.join(self.rundir + "/../config/","loginconfig.json")), encoding='utf-8')
        self.loadFromJson(cf)
       
    def loadFromJson(self, cf):
        logging.debug(u"loginserver parse serverconfig.json")
        dbstr = cf.get("game_dbe")
        if dbstr==None:
            dbstr = cf.get("game_db")
        else:
            dbstr = Utils.dencryptDbStr(dbstr)
        dbparams = dbstr.split(u',')
        self.game_db_name = dbparams[2]
        self.game_db_host = dbparams[0]
        self.game_db_port = int(dbparams[1])
        self.game_db_user = dbparams[3]
        self.game_db_passwd = dbparams[4]
        self.game_db_linkcount = int(dbparams[5]) 

        dbstr = cf.get("platform_dbe")
        if dbstr==None:
            dbstr = cf.get("platform_db")
        else:
            dbstr = Utils.dencryptDbStr(dbstr)
        dbparams = dbstr.split(u',')
        self.platform_db_name = dbparams[2]
        self.platform_db_host = dbparams[0]
        self.platform_db_port = int(dbparams[1])
        self.platform_db_user = dbparams[3]
        self.platform_db_passwd = dbparams[4]
        self.platform_db_linkcount = int(dbparams[5])

        self.server_ip = cf.get("server_ip")
        self.server_port = cf.get("server_port") 
        self.public_ip = cf.get("public_ip")
        self.public_port = cf.get("public_port")
        self.login_manager_ip = cf.get("login_manager_ip")
        self.login_manager_port = cf.get("login_manager_port")
        self.max_client = cf.get("max_client")
        self.log_ip = cf.get("log_ip")
        self.log_port = cf.get("log_port") 
        self.web_ip = cf.get("web_ip")
        self.web_port = cf.get("web_port")   

        self.center_ip = cf.get("center_ip")
        self.center_port = cf.get("center_port")

        dbstr = cf.get("redis_db")
        dbparams = dbstr.split(u',')
        self.redisdb_ip =dbparams[0]
        self.redisdb_port=int(dbparams[1])
        self.redisdb_db= int(dbparams[2])
        self.redisdb_passwd=dbparams[3] if dbparams[3]!="0" else None

        
        
   
instance = Config()